﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH;
using Module.Category.BL;
using QLBH.DTO;
using Module.Category.Model;
using QLBH.Model.Controller;

namespace Module.Category
{
    public partial class ProductManagement : ViewBase
    {
        public ProductManagement()
        {
            InitializeComponent();
        }

        private void ProductManagement_Load(object sender, EventArgs e)
        {
            LoadProducts();
        }
        private void LoadProducts()
        {
            try
            {
                dataGridViewX1.DataSource = ProductManagementBL.Instance.GetAllProducts();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AddProductDlg addProduct = new AddProductDlg();
                if (addProduct.ShowDialog()==DialogResult.OK)
                {
                    LoadProducts();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditProvider();

        }
        private void EditProvider()
        {
            try
            {
                AddProductDlg editProductDlg = new AddProductDlg();
                editProductDlg.DialogType = eDialogType.DL_EDIT;

                ProductModel selectedProduct=dataGridViewX1.CurrentRow.DataBoundItem as ProductModel;
                editProductDlg.NewProduct = ProductController.Instance.FindBy(p => p.Id == selectedProduct.Id).FirstOrDefault().ToDTO();

                if (editProductDlg.ShowDialog() == DialogResult.OK)
                {
                    LoadProducts();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditProvider();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {                
                foreach (DataGridViewRow row in dataGridViewX1.SelectedRows)
                {
                    ProductModel item = row.DataBoundItem as ProductModel;
                    ProductController.Instance.Delete(p => p.Id == item.Id);
                }
                LoadProducts();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadProducts();
        }

        private void dataGridViewX1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridViewX1.RowCount == 0)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
            }
            else
            {
                buttonEdit.Enabled = true;
                buttonDelete.Enabled = true;
            }
        }
    }
}
